<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>One-table examples</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="sdodasrel.metadata.html">Specifying the metadata</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="sdodasrel.examples.two-table.html">Two-table examples</a></div>
 <div class="up"><a href="sdodasrel.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="sdodasrel.examples.one-table" class="section">
  <h2 class="title">One-table examples</h2>
  <p class="para">
   The following set of examples all use the Relational DAS to work with 
   a data graph containing just one application data object, a single 
   company and the data just to be found the company table. These examples 
   do not exercise the power of SDO or the Relational DAS and of course 
   the same result could be achieved more economically with direct SQL 
   statements but they are intended to illustrate how to work with the 
   Relational DAS.
  </p>

  <p class="para">
   For this very simple scenario it would be possible to simplify the 
   database metadata to include just the company table - if that were done
   the second and third arguments to the constructor and the column 
   specifier used in the query example would become optional.
  </p>

  <p class="para">
   <div class="example" id="example-5348">
    <p><strong>Example #1 Creating a data object</strong></p>
    <div class="example-contents"><p>
     The simplest example is that of creating a single data object and 
     writing it to the database. In this example a single company object 
     is created, its name is set to &#039;Acme&#039;, and the Relational DAS is 
     called to write the changes to the database. The company name is 
     set here using the property name method. See the
     <a href="sdo.examples.html" class="link">Examples</a>
     section on the SDO extension for other ways of accessing the 
     properties of an object.
    </p></div>

    <div class="example-contents"><p>
     Data objects can only be created when you have a data object to 
     start with, however. It is for that reason that the first call 
     to the Relational DAS here is to obtain a root object. This is 
     in effect how to ask for an empty data graph - the special root 
     object is the true root of the tree. The company data object is 
     then created with a call to
     <span class="function"><strong>createDataObject()</strong></span>
     on the root object. This creates the company data object and inserts 
     it in the graph by inserting into a multi-valued containment property 
     on the root object called &#039;company&#039;.
    </p></div>
    <div class="example-contents"><p>
     When the Relational DAS is called to apply the changes a simple 
     insert statement &#039;INSERT INTO company (name) VALUES (&quot;Acme&quot;);&#039; 
     will be constructed and executed. The auto-generated primary key 
     will be set into the data object and the change summary will be reset, 
     so that it would be possible to continue working with the same data 
     object, modify it, and apply the newer changes a second time.
    </p></div>
                      
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">require_once&nbsp;</span><span style="color: #DD0000">'SDO/DAS/Relational.php'</span><span style="color: #007700">;<br />require_once&nbsp;</span><span style="color: #DD0000">'company_metadata.inc.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Construct&nbsp;the&nbsp;DAS&nbsp;with&nbsp;the&nbsp;metadata<br />***************************************************************/<br /></span><span style="color: #0000BB">$das&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SDO_DAS_Relational&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$database_metadata</span><span style="color: #007700">,</span><span style="color: #DD0000">'company'</span><span style="color: #007700">,</span><span style="color: #0000BB">$SDO_containment_metadata</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Obtain&nbsp;a&nbsp;root&nbsp;object&nbsp;and&nbsp;create&nbsp;a&nbsp;company&nbsp;object&nbsp;underneath.<br />*&nbsp;Make&nbsp;a&nbsp;simple&nbsp;change&nbsp;to&nbsp;the&nbsp;data&nbsp;object.&nbsp;<br />***************************************************************/<br /></span><span style="color: #0000BB">$root&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$das&nbsp;&nbsp;</span><span style="color: #007700">-&gt;&nbsp;</span><span style="color: #0000BB">createRootDataObject</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$acme&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$root&nbsp;</span><span style="color: #007700">-&gt;&nbsp;</span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'company'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$acme</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Acme"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Get&nbsp;a&nbsp;database&nbsp;connection&nbsp;and&nbsp;write&nbsp;the&nbsp;object&nbsp;to&nbsp;the&nbsp;database<br />***************************************************************/<br /></span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO_DSN</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_USER</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_PASSWORD</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$das&nbsp;</span><span style="color: #007700">-&gt;&nbsp;</span><span style="color: #0000BB">applyChanges</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$root</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5349">
    <p><strong>Example #2 Retrieving a data object</strong></p>
    <div class="example-contents"><p>
     In this example a single data object is retrieved from the database 
     - or possibly more than one if there is more than one company 
     called &#039;Acme&#039;. For each company returned, the
     <var class="varname"><var class="varname">name</var></var>
     and
     <var class="varname"><var class="varname">id</var></var>
     properties are echoed.
    </p></div>
    <div class="example-contents"><p>
     In this example the third argument to
     <span class="function"><strong>executeQuery()</strong></span>,
     the column specifier is needed as there are other tables in the 
     metadata with column names of
     <var class="varname"><var class="varname">name</var></var>
     and
     <var class="varname"><var class="varname">id</var></var>.
     If there were no possible ambiguity it could be omitted.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">require_once&nbsp;</span><span style="color: #DD0000">'SDO/DAS/Relational.php'</span><span style="color: #007700">;<br />require_once&nbsp;</span><span style="color: #DD0000">'company_metadata.inc.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Construct&nbsp;the&nbsp;DAS&nbsp;with&nbsp;the&nbsp;metadata<br />***************************************************************/<br /></span><span style="color: #0000BB">$das&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SDO_DAS_Relational&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$database_metadata</span><span style="color: #007700">,</span><span style="color: #DD0000">'company'</span><span style="color: #007700">,</span><span style="color: #0000BB">$SDO_containment_metadata</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Get&nbsp;a&nbsp;database&nbsp;connection<br />***************************************************************/<br /></span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO_DSN</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_USER</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_PASSWORD</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Issue&nbsp;a&nbsp;query&nbsp;to&nbsp;obtain&nbsp;a&nbsp;company&nbsp;object&nbsp;-&nbsp;possibly&nbsp;more&nbsp;if&nbsp;they&nbsp;exist<br />***************************************************************/<br /></span><span style="color: #0000BB">$root&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$das</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'select&nbsp;name,&nbsp;id&nbsp;from&nbsp;company&nbsp;where&nbsp;name="Acme"'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'company.name'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'company.id'</span><span style="color: #007700">)&nbsp;);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Echo&nbsp;name&nbsp;and&nbsp;id&nbsp;<br />***************************************************************/<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$root</span><span style="color: #007700">[</span><span style="color: #DD0000">'company'</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Company&nbsp;obtained&nbsp;from&nbsp;the&nbsp;database&nbsp;has&nbsp;name&nbsp;=&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;and&nbsp;id&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5350">
    <p><strong>Example #3 Updating a data object</strong></p>
    <div class="example-contents"><p>
     This example combines the previous two, in the sense that in order 
     to be updated the object must first be retrieved. The application 
     code reverses the company name (so &#039;Acme&#039; becomes &#039;emcA&#039;) and then the
     changes are written back to the database in the same way that they 
     were when the object was created. Because the query searches for 
     the name both ways round the program can be run repeatedly to find 
     the company and reverse its name each time.
    </p></div>
    <div class="example-contents"><p>
     In this example the same instance of the Relational DAS is reused 
     for the
     <span class="function"><strong>applyChanges()</strong></span>,
     as is the PDO database handle. This is quite alright; it also 
     alright to allow the previous instances to be garbage collected 
     and to obtain new instances. No state data regarding the graph 
     is held the Relational DAS once it has returned a data graph to 
     the application. All necessary data is either within the graph itself, 
     or can be reconstructed from the metadata.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">require_once&nbsp;</span><span style="color: #DD0000">'SDO/DAS/Relational.php'</span><span style="color: #007700">;<br />require_once&nbsp;</span><span style="color: #DD0000">'company_metadata.inc.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Construct&nbsp;the&nbsp;DAS&nbsp;with&nbsp;the&nbsp;metadata<br />***************************************************************/<br /></span><span style="color: #0000BB">$das&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SDO_DAS_Relational&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$database_metadata</span><span style="color: #007700">,</span><span style="color: #DD0000">'company'</span><span style="color: #007700">,</span><span style="color: #0000BB">$SDO_containment_metadata</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Get&nbsp;a&nbsp;database&nbsp;connection<br />***************************************************************/<br /></span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO_DSN</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_USER</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_PASSWORD</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Issue&nbsp;a&nbsp;query&nbsp;to&nbsp;obtain&nbsp;a&nbsp;company&nbsp;object&nbsp;-&nbsp;possibly&nbsp;more&nbsp;if&nbsp;they&nbsp;exist<br />***************************************************************/<br /></span><span style="color: #0000BB">$root&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$das</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'select&nbsp;name,&nbsp;id&nbsp;from&nbsp;company&nbsp;where&nbsp;name="Acme"&nbsp;or&nbsp;name="emcA"'</span><span style="color: #007700">,<br />&nbsp;&nbsp;array(</span><span style="color: #DD0000">'company.name'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'company.id'</span><span style="color: #007700">)&nbsp;);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Alter&nbsp;the&nbsp;name&nbsp;of&nbsp;just&nbsp;the&nbsp;first&nbsp;company<br />***************************************************************/<br /></span><span style="color: #0000BB">$company&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$root</span><span style="color: #007700">[</span><span style="color: #DD0000">'company'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #DD0000">"obtained&nbsp;a&nbsp;company&nbsp;with&nbsp;name&nbsp;of&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strrev</span><span style="color: #007700">(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Write&nbsp;the&nbsp;change&nbsp;back<br />***************************************************************/<br /></span><span style="color: #0000BB">$das</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">applyChanges</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">,</span><span style="color: #0000BB">$root</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5351">
    <p><strong>Example #4 Deleting a data object</strong></p>
    <div class="example-contents"><p>
     Any companies called &#039;Acme&#039; or its reverse &#039;emcA&#039; are retrieved.
     They are then all deleted from the graph with unset.
    </p></div>

    <div class="example-contents"><p>
     In this example they are all deleted in one go by unsetting the 
     containing property (the property defining the containment 
     relationship). It is also possible to delete them individually.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">require_once&nbsp;</span><span style="color: #DD0000">'SDO/DAS/Relational.php'</span><span style="color: #007700">;<br />require_once&nbsp;</span><span style="color: #DD0000">'company_metadata.inc.php'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Construct&nbsp;the&nbsp;DAS&nbsp;with&nbsp;the&nbsp;metadata<br />***************************************************************/<br /></span><span style="color: #0000BB">$das&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SDO_DAS_Relational&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$database_metadata</span><span style="color: #007700">,</span><span style="color: #DD0000">'company'</span><span style="color: #007700">,</span><span style="color: #0000BB">$SDO_containment_metadata</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Get&nbsp;a&nbsp;database&nbsp;connection<br />***************************************************************/<br /></span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO_DSN</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_USER</span><span style="color: #007700">,</span><span style="color: #0000BB">DATABASE_PASSWORD</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Issue&nbsp;a&nbsp;query&nbsp;to&nbsp;obtain&nbsp;a&nbsp;company&nbsp;object&nbsp;-&nbsp;possibly&nbsp;more&nbsp;if&nbsp;they&nbsp;exist<br />***************************************************************/<br /></span><span style="color: #0000BB">$root&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$das</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'select&nbsp;name,&nbsp;id&nbsp;from&nbsp;company&nbsp;where&nbsp;name="Acme"&nbsp;or&nbsp;name="emcA"'</span><span style="color: #007700">,<br />&nbsp;&nbsp;array(</span><span style="color: #DD0000">'company.name'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'company.id'</span><span style="color: #007700">)&nbsp;);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Delete&nbsp;any&nbsp;companies&nbsp;found&nbsp;from&nbsp;the&nbsp;data&nbsp;graph<br />***************************************************************/<br /></span><span style="color: #007700">unset(</span><span style="color: #0000BB">$root</span><span style="color: #007700">[</span><span style="color: #DD0000">'company'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">/**************************************************************<br />*&nbsp;Write&nbsp;the&nbsp;change(s)&nbsp;back<br />***************************************************************/<br /></span><span style="color: #0000BB">$das</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">applyChanges</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">,</span><span style="color: #0000BB">$root</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="sdodasrel.metadata.html">Specifying the metadata</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="sdodasrel.examples.two-table.html">Two-table examples</a></div>
 <div class="up"><a href="sdodasrel.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
